package com.shopnight.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopnight.entity.AppleService;
import com.shopnight.entity.ShopCategory;
import com.shopnight.entity.ShopSlideshow;
import com.shopnight.mapper.AppleServiceMapper;
import com.shopnight.result.ApiResult;
import com.shopnight.result.ApiResultList;
import com.shopnight.service.*;
import com.shopnight.utils.MyMQTTClient;
import com.shopnight.utils.PageUtils;
import com.shopnight.vo.PrizesVO;
import com.shopnight.vo.ResByIdOrder;
import com.shopnight.vo.ResOrder;
import com.shopnight.vo.UserRankedVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
@Api(tags = {"IndexController"},description = "首页面所需接口")
public class IndexController {
    @Autowired
    ShopCategoryService shopCategoryService;

    @Autowired
    ShopOrderService shopOrderService;

    @Autowired
    ShopSlideshowService shopSlideshowService;

    @Autowired
    UserService userService;

    @Autowired
    RankedPrizeService rankedPrizeService;


    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private AppleServiceMapper appleServiceMapper;

    @GetMapping("/getSlideshow")
    @ApiOperation(value = "获取轮播图",notes = "需要组织id")
    public  ApiResultList<ShopSlideshow> getSlideshow(@RequestParam Integer orgId){
        return  ApiResultList.ok( shopSlideshowService.list(
                new LambdaQueryWrapper<ShopSlideshow>().eq(ShopSlideshow::getOrgId,orgId)
        ) );
    }

    @GetMapping("/getCategory")
    @ApiOperation(value = "获取所有分类",notes = "需要组织id")
    public  ApiResultList<ShopCategory> getCategory(@RequestParam Integer orgId){
       return ApiResultList.ok( shopCategoryService.list(
               new LambdaQueryWrapper<ShopCategory>().eq(ShopCategory::getOrgId,orgId)
       ) );
    }

    @PostMapping("/getCategoryName")
    @ApiOperation(value = "获取分类名字",notes = "需传分类id ")
    public  ApiResult<ShopCategory> getCategoryName(@ApiParam("参数为:CategoryId") @RequestBody Map<String,Integer> map){
        return  ApiResult.ok( shopCategoryService.getById(map.get("CategoryId")) );
    }

    @PostMapping("/getOrderByCategoryId")
    @ApiOperation(value = "获取某个分类下的商品",notes = "需传当前页码 页码数 分类id ")
    public  ApiResult<Page<ResOrder>> getOrderByCategoryId(@ApiParam("参数为：page pageSize CategoryId")  @RequestBody Map<String,Integer> map){
        Page<ResOrder> page =  new Page<>(  map.get("page"), map.get("pageSize") );
        return ApiResult.ok( shopOrderService.getOrderByCategoryId(page, map.get("CategoryId")) );
    }
    @PostMapping("/getOrder")
    @ApiOperation(value = "获取所有商品",notes = "需传组织 当前页码 页码数 ")
    public  ApiResult<Page<ResOrder>> getOrder(@ApiParam("参数为：orgId page pageSize")  @RequestBody Map<String,Integer> map){
        Page<ResOrder> page =  new Page<>(  map.get("page"), map.get("pageSize") );
        return ApiResult.ok( shopOrderService.getOrder(map.get("orgId"),page));
    }

    @PostMapping("/getOrderByName")
    @ApiOperation(value = "根据商品名模糊匹配商品",notes = "需传组织id 当前页码 页码数 模糊关键字")
    public  ApiResult<Page<ResOrder>> getOrderByName(@ApiParam("参数为：orgId page pageSize orderName") @RequestBody Map<String,Object> map){
        Page<ResOrder> page =  new Page<>( Long.valueOf(map.get("page").toString()) , Long.valueOf( map.get("pageSize").toString()));
        return ApiResult.ok(shopOrderService.getOrderByName((Integer) map.get("orgId"),page, (String) map.get("orderName")));
    }
    @PostMapping("/getOrderById")
    @ApiOperation(value = "根据商品id获取详细信息",notes = "需传商品id")
    public ApiResult<Object> getOrderById(@ApiParam("参数为：orderId ") @RequestBody Map<String,Integer> map){
        ResByIdOrder order = shopOrderService.getOrderById(map.get("orderId"));
        return ApiResult.ok( JSONObject.toJSON(order) );
    }
    @PostMapping("/getSeckillOrder")
    @ApiOperation(value = "查询所有秒杀商品",notes = "需传组织id 当前页码 页码数")
    public ApiResult<Page<ResOrder>> getSeckillOrder(@ApiParam("参数为：orgId page pageSize") @RequestBody Map<String,Integer> map){
        Page<ResOrder> page =  new Page<>(  map.get("page"), map.get("pageSize") );
        return ApiResult.ok( shopOrderService.getSeckillOrder(map.get("orgId"),page) );
    }
    @GetMapping("/getSeckillModule")
    @ApiOperation(value = "查询销量前六的秒杀商品图片和价格",notes = "需传组织id")
    public ApiResult<List<Map<String,Object>>> getSeckillModule(@ApiParam("参数为：orgId") @RequestParam Integer orgId){
        return ApiResult.ok( shopOrderService.getSeckillModule(orgId) );
    }
    @PostMapping("/getUserRanked")
    @ApiOperation(value = "查询用户消费排名",notes = "无需传参")
    public ApiResult<List<UserRankedVO>> getUserRanked(){
        return ApiResult.ok( userService.getUserRanked() );
    }
    @PostMapping("/getRankedPrize")
    @ApiOperation(value = "获取奖品",notes = "无需传参")
    public ApiResult<List<PrizesVO>> getRankedPrize(){
        return ApiResult.ok( rankedPrizeService.getRankedPrize() );
    }

    @GetMapping("/getOrganizationTwoInfo/{id}")
    @ApiOperation(value = "获取组织详细信息",notes = "传组织id")
    public ApiResult getOrganizationTwoInfo(@PathVariable("id") Integer id){
       return ApiResult.ok(organizationService.getOrganizationTwoInfo(id));
    }


    @GetMapping("/getAppleService")
    @ApiOperation(value = "获取下次小程序首页服务",notes = "无需传参")
    public ApiResult getAppleService(){
        return ApiResult.ok(appleServiceMapper.selectList(
                new LambdaQueryWrapper<AppleService>().eq(AppleService::getIsShow,1)
        ));
    }



}
